/*
 Copyright 2009 Christian Sprecher

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
package helpers

import widgets.JcrDefaultMutableTreeNode
import javax.jcr.Node as JcrNode
import javax.swing.tree.TreeNode
import javax.swing.tree.MutableTreeNode
import javax.swing.JTree
import javax.jcr.Session

/**
 * Created by IntelliJ IDEA.
 * User: christian
 * Date: Dec 29, 2008
 * Time: 8:14:36 PM
 * To change this template use File | Settings | File Templates.
 */

public class JcrTreeHelper {
    JTree myTree
    JcrHelper helper
    public JcrTreeHelper(JTree tree, Session session) {
      myTree = tree
      helper = new JcrHelper (session)
//      this.session = session
    }

    //TODO: this might not perform
    public void addChildNode(TreeNode parentTreeModelNode, String path) {
        helper.withNode(path) { node ->
            JcrDefaultMutableTreeNode childTreeModelNode = new JcrDefaultMutableTreeNode(path, node.hasNodes())
            parentTreeModelNode.add(childTreeModelNode)
        }
//        helper.withNode(path) { node ->
//            node.nodes.each { childNode ->
//                 addChildNode(childTreeModelNode,childNode.path)
//            }
//        }
    }

    /**
    /**
    * targetParentTreeNode.userObject must be a JCR Node
     */
    String moveNode(MutableTreeNode targetParentTreeNode, MutableTreeNode sourceNode) {
        def targetNodePath = targetParentTreeNode.userObject
        def result
        helper.withNode(targetNodePath) { targetNode ->
            int index = targetNode.nodes.size

            TreeNode sourceParent = sourceNode.parent
            int sourceIndex = sourceParent.getIndex(sourceNode)

            

            def ws = targetNode.session.workspace
            def targetPath =targetNode.path + "/" + sourceNode.name
            ws.move(sourceNode.userObject,targetPath)
            addChildNode(targetParentTreeNode, targetPath)
            myTree.model.nodesWereInserted(targetParentTreeNode, [index] as int[])
            myTree.model.nodesWereRemoved(sourceParent, [sourceIndex] as int[], [sourceNode] as Object[])
            result = targetPath
        }
        return result
    }
    /**
    /**
    * targetParentTreeNode.userObject must be a JCR Node
     */
    String copyNode(MutableTreeNode targetParentTreeNode, MutableTreeNode sourceNode) {
        def targetNodePath = targetParentTreeNode.userObject
        def result
        helper.withNode(targetNodePath) { targetNode ->
            int index = targetNode.nodes.size
            def ws = targetNode.session.workspace
            def targetPath =targetNode.path + "/" + sourceNode.name
            ws.copy(sourceNode.userObject,targetPath)
            addChildNode(targetParentTreeNode, targetPath)
            myTree.model.nodesWereInserted(targetParentTreeNode, [index] as int[])
            result = targetPath
        }
        return result
    }

    /**
    * currentTreeNode.userObject must be a JCR Node, nodetype default is nt:unstructured
     */
    String addNode(MutableTreeNode parentTreeNode, String childName, String nodeType = "nt:unstructured") {
        def currentNodePath = parentTreeNode.userObject
        String result
        helper.withNode(currentNodePath) { currentNode ->
            int index = currentNode.nodes.size
            JcrNode newNode = currentNode.addNode(childName, nodeType)
            addChildNode(parentTreeNode,newNode.path)
            currentNode.session.save()
            myTree.model.nodesWereInserted(myTree.lastSelectedPathComponent, [index] as int[])
            result = newNode.path
        }
        return result
    }
    /**
    * currentTreeNode.userObject must be a JCR Node, nodetype default is nt:unstructured
     */
    String renameNode(MutableTreeNode treeNode, String newName) {
        def currentNodePath = treeNode.userObject
        def result
        helper.withNode (currentNodePath) { currentNode ->
            int index = currentNode.nodes.size
            currentNode.session.move(currentNode.path, currentNode.parent.path+ "/" + newName);
            currentNode.session.save()
            myTree.model.nodeChanged(treeNode)
        }
        return newName
    }
}